/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 28.07.2008
*/

package it.cums.ui.wizards;

import it.cums.core.controller.ApplicationController;
import it.cums.core.model.Customer;
import it.cums.core.model.Customers;
import it.cums.core.model.Order;
import it.cums.core.util.Converter;
import it.cums.ui.localization.Messages;

import java.util.Date;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class OrderWizardPage extends WizardPage implements IWizardPage {
	private Customer customer;
	private Combo statusCombo;
	private Combo customerCombo;
	private Combo priorityCombo;
	private Text orderDateText;
	private Text deliveryDateText;
	private Composite container;
	
	public OrderWizardPage() {
		super(Messages.OrderWizardPage_0);
		setTitle(Messages.OrderWizardPage_1);
		setDescription(Messages.OrderWizardPage_2);
	}
	
	public OrderWizardPage(Customer customer) {
		super(Messages.OrderWizardPage_3);
		this.customer = customer;
		setTitle(Messages.OrderWizardPage_4);
		setDescription(Messages.OrderWizardPage_5);
	}

	
	public void createControl(Composite parent) {
		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		Label label1 = new Label(container, SWT.NULL);
		label1.setText(Messages.OrderWizardPage_6);
		
		statusCombo = new Combo(container, SWT.READ_ONLY);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		for (String status : Order.STATUS) {
			statusCombo.add(status);
		}
		statusCombo.setLayoutData(gd);
		statusCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText(Messages.OrderWizardPage_7);
		
		customerCombo = new Combo(container, SWT.READ_ONLY);
		customerCombo.setLayoutData(gd);
		Customers customers = ApplicationController.getInstance().getCustomers();
		for (Customer customer : customers.getCustomers()) {
			customerCombo.add(customer.getName());
		}
		if (customer != null) {
			customerCombo.setText(customer.getName());
		}
		customerCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label3 = new Label(container, SWT.NULL);
		label3.setText(Messages.OrderWizardPage_8);
		
		priorityCombo = new Combo(container, SWT.READ_ONLY);
		priorityCombo.setLayoutData(gd);
		for (String priority : Order.PRIORITY) {
			priorityCombo.add(priority);
		}
		priorityCombo.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label4 = new Label(container, SWT.NULL);
		label4.setText(Messages.OrderWizardPage_9);
		
		orderDateText = new Text(container, SWT.BORDER | SWT.SINGLE);
		orderDateText.setLayoutData(gd);
		orderDateText.setText(Converter.convertToString(new Date()));
		orderDateText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		Label label5 = new Label(container, SWT.NULL);
		label5.setText(Messages.OrderWizardPage_10);
		
		deliveryDateText = new Text(container, SWT.BORDER | SWT.SINGLE);
		deliveryDateText.setLayoutData(gd);
		deliveryDateText.addModifyListener(new ModifyListener() {
			
			public void modifyText(ModifyEvent arg0) {
				dialogChanged();
			}
        });
		
		setControl(container);
		setPageComplete(false);
	}
	
	private void dialogChanged() {
		boolean complete = true;
		if (getStatus().length() == 0) {
			setDescription(Messages.OrderWizardPage_11);
			setErrorMessage(null);
			complete = false;
		} 
		if (getCustomer().length() == 0) {
			setDescription(Messages.OrderWizardPage_12);
			setErrorMessage(null);
			complete = false;
		} 
		if (getPriority().length() == 0) {
			setDescription(Messages.OrderWizardPage_13);
			setErrorMessage(null);
			complete = false;
		} 
		if (complete && getOrderDate().length() != 0) {
			try {
				Converter.convertToDate(getOrderDate());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage(Messages.OrderWizardPage_14);
			}
		} 
		if (complete && getDeliveryDate().length() != 0) {
			try {
				Converter.convertToDate(getDeliveryDate());
				setErrorMessage(null);
			} catch (Exception e) {
				complete = false;
				setErrorMessage(Messages.OrderWizardPage_15);
			}
		} 
		if (complete) {
			setDescription(null);
			setErrorMessage(null);
		}
		setPageComplete(complete);
	}
	
	public String getCustomer() {
		return customerCombo.getText().trim();
	}
	
	public String getStatus() {
		return statusCombo.getText().trim();
	}
	
	public String getPriority() {
		return priorityCombo.getText().trim();
	}
	
	public String getOrderDate() {
		return orderDateText.getText().trim();
	}
	
	public String getDeliveryDate() {
		return deliveryDateText.getText().trim();
	}
	
	
	public Control getControl() {
		return container;
	}
}
